﻿//visitViewModel constructor
function visitViewModel(mapVisitList) {
    var visit = {
        //Properties
        Picture: ko.observable('')
        , Url: ko.observable('')
        , SumUp: ko.observable('')
        , Title: ko.observable('')
        , isLoaded: ko.observable(false)
        //Methods
        ,
        //Build informations from a MapVisitList object
        load: function (mapVisitList) {
            if (mapVisitList != null) {
                this.Picture(mapVisitList.Picture);
                this.Url(mapVisitList.Url);
                this.SumUp(mapVisitList.SumUp);
                this.Title(mapVisitList.Title);
                this.isLoaded(true);
            }
        }
    }
    return visit;
}

//visitPageViewModel constructor
function visitPageViewModel(pageNumber) {
    var page = {
        //Gets/sets the number of the pages
        number: ko.observable(pageNumber)
        , //Gets/sets a boolean indicatings whether the page is already loaded
        isLoaded: ko.observable(false)
        , //Gets/sets a boolean indicatings whether the page is loading
        isLoading: ko.observable(false)
    }
    return page;
}

//Page viewModel
var viewModel = {
    //Gets/sets a boolean indicatings whether datas are loading
    isLoading: ko.observable(false)
    , isLoaded: ko.observable(false)
    , //Gets/sets the number of rows per page
    rowsPerPage: ko.observable(30)
    , //Gets/sets the number of rows per page
    numberOfRows: ko.observable(0)
    , numberOfPages: ko.observable(0)
    , visits: ko.observableArray([])
    , pages: ko.observableArray([])
    , //Calls the server to get the total number of rows to display
    getNumberOfVisits: function () {
        this.isLoading(true);

        var service = new mal.balades.Web.Services.VisitServices();
        service.GetNumberOfVisits(this.onGetNumberOfVisitsSuccess, null, null);

        //$.ajax({ type: 'POST', url: '/VisitsService.svc/GetNumberOfVisits', dataType: 'json', success: this.onGetNumberOfVisitsSuccess });

    }

    , //Method called when the server responses to the GetNumberOfVisits request
    // Builds the list of pages and loads the first page.
    onGetNumberOfVisitsSuccess: function (result) {
        // result = result.d;
        viewModel.isLoading(false);
        viewModel.numberOfRows(result);
        viewModel.visits([]);
        for (var i = 0; i < result; i++) {
            viewModel.visits.push(visitViewModel());
        }

        viewModel.buildPages();

        viewModel.loadPage(0);
    }

    , //Builds the list of pages
    buildPages: function () {
        viewModel.pages([]);

        var numberOfPages = 0;

        numberOfPages = Math.round(viewModel.numberOfRows() / viewModel.rowsPerPage());

        if (numberOfPages != viewModel.numberOfRows() / viewModel.rowsPerPage())
            numberOfPages++;

        viewModel.numberOfPages(numberOfPages);

        for (var i = 0; i < numberOfPages; i++) {
            viewModel.pages.push(visitPageViewModel(i));
        }
    }

    , //Loads the page "pageNumber" if it has not been loaded before.
    loadPage: function (pageNumber) {

        var page;
        for (var i = 0; i < viewModel.pages().length; i++) {
            if (i == pageNumber) {
                if (!viewModel.pages()[i].isLoaded() && !viewModel.pages()[i].isLoading()) {
                    page = viewModel.pages()[i];
                }
            }
        }

        if (page != null) {

            this.isLoading(true);
            page.isLoading(true);
            var service = new mal.balades.Web.Services.VisitServices();
            service.GetVisits(page.number(), this.rowsPerPage(), this.onGetVisitsSuccess, this.onGetVisitsError, page);
        }
    }
    , //Method calls when the server responses to the GetVisits request 
    onGetVisitsSuccess: function (result, page) {

        for (var i = 0; i < result.length; i++) {
            viewModel.visits()[(page.number() * viewModel.rowsPerPage()) + i].load(result[i]);
        }

        page.isLoading(false);
        page.isLoaded(true);

        viewModel.setIsLoading();
        viewModel.isLoaded(true);
    }

     ,
    onGetVisitsError: function (result) {
        alert(result);
    }
    , setIsLoading: function () {
        for (var i = 0; i < viewModel.pages().length; i++) {
            if (viewModel.pages()[i].isLoading()) {
                viewModel.isLoading(true);
                return;
            }
        }

        viewModel.isLoading(false);
    }

    , loadPages: function () {
        var totalHeight = $('#innerContainer').innerHeight();
        var currentPostion = $('#container').scrollTop();

        var itemIndex = currentPostion * viewModel.numberOfRows() / totalHeight;
        var pageNumber = Math.round((itemIndex / viewModel.rowsPerPage()));

        viewModel.loadPage(pageNumber);
        viewModel.loadPage(pageNumber - 1);
        viewModel.loadPage(pageNumber + 1);
    }

}